﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChessAnotator.Base.Business;

namespace ChessAnotator.UserControls
{
    public class Position
    {
        #region Private Members
        private string m_sFENPosition;
        #endregion

        public Position(string sFENPosition)
        {
            m_sFENPosition = sFENPosition;
        }

        public List<Pieces.BasePiece> GetPieces(double nPieceScale)
        {
            List<Pieces.BasePiece> arrResult = new List<Pieces.BasePiece>();

            string[] ranks = m_sFENPosition.Split(' ');
            ranks = ranks[0].Split('/');
            for (int nRank = 0; nRank < Nomenclature.BOARD_SIZE; nRank++)
            {
                int nFile = 1;
                foreach (char ch in ranks[nRank])
                {
                    int nBlankSquares;
                    if (int.TryParse(ch.ToString(), out nBlankSquares))
                    {
                        nFile += nBlankSquares;
                        continue;
                    }

                    Pieces.BasePiece oCurrPiece = null;

                    switch (ch.ToString())
                    {
                        case Nomenclature.FENChessPiece.BlackPawn:
                            oCurrPiece = new Pieces.BlackPawn();
                            break;

                        case Nomenclature.FENChessPiece.BlackRook:
                            oCurrPiece = new Pieces.BlackRook();
                            break;

                        case Nomenclature.FENChessPiece.BlackKnight:
                            oCurrPiece = new Pieces.BlackKnight();
                            break;

                        case Nomenclature.FENChessPiece.BlackBishop:
                            oCurrPiece = new Pieces.BlackBishop();
                            break;

                        case Nomenclature.FENChessPiece.BlackKing:
                            oCurrPiece = new Pieces.BlackKing();
                            break;

                        case Nomenclature.FENChessPiece.BlackQueen:
                            oCurrPiece = new Pieces.BlackQueen();
                            break;

                        // White pieces
                        case Nomenclature.FENChessPiece.WhitePawn:
                            oCurrPiece = new Pieces.WhitePawn();
                            break;

                        case Nomenclature.FENChessPiece.WhiteRook:
                            oCurrPiece = new Pieces.WhiteRook();
                            break;

                        case Nomenclature.FENChessPiece.WhiteKnight:
                            oCurrPiece = new Pieces.WhiteKnight();
                            break;

                        case Nomenclature.FENChessPiece.WhiteBishop:
                            oCurrPiece = new Pieces.WhiteBishop();
                            break;

                        case Nomenclature.FENChessPiece.WhiteKing:
                            oCurrPiece = new Pieces.WhiteKing();
                            break;

                        case Nomenclature.FENChessPiece.WhiteQueen:
                            oCurrPiece = new Pieces.WhiteQueen();
                            break;
                    }

                    if (oCurrPiece != null)
                    {
                        oCurrPiece.SquareName = GetSquare(nFile, Nomenclature.BOARD_SIZE - nRank);

                        arrResult.Add(oCurrPiece);
                    }

                    nFile++;
                }
            }

            return arrResult;
        }

        private string GetSquare(int nFile, int nRank)
        {
            string sFile = "";

            switch (nFile)
            { 
                case 1:
                    sFile = Nomenclature.Rank.A;
                    break;
                case 2:
                    sFile = Nomenclature.Rank.B;
                    break;
                case 3:
                    sFile = Nomenclature.Rank.C;
                    break;
                case 4:
                    sFile = Nomenclature.Rank.D;
                    break;
                case 5:
                    sFile = Nomenclature.Rank.E;
                    break;
                case 6:
                    sFile = Nomenclature.Rank.F;
                    break;
                case 7:
                    sFile = Nomenclature.Rank.G;
                    break;
                case 8:
                    sFile = Nomenclature.Rank.H;
                    break;
            }

            return (string)typeof(Nomenclature.Square).GetField(sFile + nRank).GetValue(null); 
        }
    }
}
